package com.lahuan.common;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author lahuan
 */
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 成功标记
     */
    private static final int SUCCESS = 200;

    /**
     * 失败标记
     */
    private static final int FAIL = -1;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    public static <T> Result<T> ok() {
        return restResult(null, SUCCESS, null);
    }

    public static <T> Result<T> ok(T data) {
        return restResult(data, SUCCESS, null);
    }

    public static <T> Result<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> Result<T> failed() {
        return restResult(null, FAIL, null);
    }

    public static <T> Result<T> failed(String msg) {
        return restResult(null,FAIL, msg);
    }

    public static <T> Result<T> failed(T data) {
        return restResult(data, FAIL, null);
    }

    public static <T> Result<T> failed(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    private static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
